Imports SchemaCodeGen

Partial Class pages_Entitys_Entity : Inherits CPage

#Region "Querystring"
	Public ReadOnly Property EntityId() As Integer
		Get
			Return CWeb.RequestInt("entityId")
		End Get
	End Property
	Public ReadOnly Property IsEdit() As Boolean
		Get
			Return EntityId <> Integer.MinValue
		End Get
	End Property
#End Region

#Region "Members"
	Private m_entity As CEntity
#End Region

#Region "Data"
	Public ReadOnly Property [Entity]() As CEntity
		Get
			If IsNothing(m_entity) Then
				If IsEdit Then
					m_entity = CEntity.Cache.GetById(EntityId)
					If IsNothing(m_entity) Then CSitemap.RecordNotFound("Entity", EntityId)
				Else
					m_entity = New CEntity
					'Inserts: set parentId here (if applicable)
				End If
			End If
			Return m_entity
		End Get
	End Property
#End Region

#Region "Navigation"
	Private Sub Refresh()
		Response.Redirect(CSitemap.EntityEdit(Me.Entity.EntityId))
	End Sub
	Private Sub ReturnToList()
		Response.Redirect(CSitemap.Entitys)
	End Sub
#End Region

#Region "Event Handlers - Page"
	Protected Overrides Sub PageInit()
		With ddEntityConnectionId
			.DataTextField = "ConnectionName"
			.DataValueField = "ConnectionId"
			.DataSource = CConnection.Cache
			.DataBind()
			.BlankItem("-- Select Connection --")
		End With



		'Page Title
		If IsEdit Then
			Me.Title = "Entity Details"
		Else
			Me.Title = "Create New Entity"
		End If

		'Textbox logic
		'Me.Form.DefaultFocus = txtEntityName.Textbox.ClientID;
		Me.Form.DefaultButton = btnSave.UniqueID   'txtEntityName.OnReturnPress(btnSave)

		'Button Text
		btnDelete.Visible = IsEdit
		If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Entity"
		If IsEdit Then AddButton(CSitemap.EntityAdd(), "Create a new Entity")
	End Sub
	Protected Overrides Sub PageLoad()
		If Page.IsPostBack Then Exit Sub

		LoadEntity()
	End Sub
#End Region

#Region "Event Handlers - Form"
	Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If Not Me.IsValid() Then Exit Sub
		SaveEntity()
		Refresh()
	End Sub
	Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
		ReturnToList()
	End Sub
	Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
		Me.Entity.Delete()
		'CCache.ClearCache()
		ReturnToList()
	End Sub
#End Region

#Region "Private - Load/Save"
	Protected Sub LoadEntity()
		With Me.Entity
			txtEntityClassName.Text = .EntityClassName
			ddEntityConnectionId.ValueInt = .EntityConnectionId
			txtEntityTableName.Text = .EntityTableName
			txtEntitySchemaName.Text = .EntitySchemaName
			chkEntityIsCached.Checked = .EntityIsCached
			chkEntityIsAudited.Checked = .EntityIsAudited
			txtEntityViewName.Text = .EntityViewName
			txtEntityOrderByColumns.Text = .EntityOrderByColumns
			txtEntityOrdinalColumn.Text = .EntityOrdinalColumn
			txtEntityCreatedColumn.Text = .EntityCreatedColumn
			txtEntityModifiedColumn.Text = .EntityModifiedColumn
			txtEntityFilterColumns.Text = .EntityFilterColumns
			txtEntityCreated.ValueDate = .EntityCreated
			txtEntityModified.ValueDate = .EntityModified
		End With
	End Sub
	Protected Sub SaveEntity()
		With Me.Entity
			.EntityClassName = txtEntityClassName.Text
			.EntityConnectionId = ddEntityConnectionId.ValueInt
			.EntityTableName = txtEntityTableName.Text
			.EntitySchemaName = txtEntitySchemaName.Text
			.EntityIsCached = chkEntityIsCached.Checked
			.EntityIsAudited = chkEntityIsAudited.Checked
			.EntityViewName = txtEntityViewName.Text
			.EntityOrderByColumns = txtEntityOrderByColumns.Text
			.EntityOrdinalColumn = txtEntityOrdinalColumn.Text
			.EntityCreatedColumn = txtEntityCreatedColumn.Text
			.EntityModifiedColumn = txtEntityModifiedColumn.Text
			.EntityFilterColumns = txtEntityFilterColumns.Text
			.EntityCreated = txtEntityCreated.ValueDate
			.EntityModified = txtEntityModified.ValueDate

			.Save()
		End With
		'CCache.ClearCache()
	End Sub
#End Region

End Class
